package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.common.enums.InpChargeIndicator;
import com.th.supcom.pds.impl.common.enums.InpDispenseIndicator;
import com.th.supcom.pds.impl.common.enums.NeedDispenseEnum;
import com.th.supcom.pds.impl.service.internal.phamapply.PhamDispenseConvertUtils;

/**
 * 通过摆药请领明细初步生成摆药执行明细
 * 
 * @author leaqua@qq.com
 */
public class GenerateDispenseDetail extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext context = (PhamDispenseContext) arg0;
        String operator = context.getOperator ();
        String dispensary = context.getDispensary ();
        List <InpPhamApplyDetailVO> applyList = context.getApplyDetailVOList ();
        List <InpPhamApplyDetailVO> notDispenseApplyList = new ArrayList <InpPhamApplyDetailVO> ();
        List <InpPhamApplyDetailVO> changeDispensaryApplyDetail = new ArrayList <InpPhamApplyDetailVO> ();
        List <InpPhamDispenseDetailVO> zeroPhamDispenseDetailList = new ArrayList <InpPhamDispenseDetailVO> ();
        List <String> phamStdCodeList = new ArrayList <String> ();
        // 首先过滤前台返回的住院请领信息
        List <InpPhamDispenseDetailVO> dispenseDetailList = new ArrayList <InpPhamDispenseDetailVO> ();
        for (InpPhamApplyDetailVO apply : applyList)
        {
            // 如果执行药房与当前药房不能，则是转向明细，跳过不处理
            if (!apply.getExecDispensary ().equals (dispensary))
            {
                apply.setDispenseIndicator (InpDispenseIndicator.Supplyed.getEnumCode ());
                apply.setNoDispenseMemo (null);
                changeDispensaryApplyDetail.add (apply);
                continue;
            }
            // 如果不摆药，跳过
            if (apply.getDispenseResultFlag ().equals (NeedDispenseEnum.NO.getEnumCode ()))
            {
                notDispenseApplyList.add (apply);
                continue;
            }
            InpPhamDispenseDetailVO dispense = PhamDispenseConvertUtils.convertApplyToDispenseDetail (apply, operator, dispensary);
            // 对于摆药数量为零的请领记录
            if (apply.getApplyAmount () == 0)
            {
                dispense.setDispenseIndicator (InpDispenseIndicator.HasDispensed.getEnumCode ());
                dispense.setChargeIndicator (InpChargeIndicator.Charged.getEnumCode ());
            }

            dispenseDetailList.add (dispense);
        }
        List <Long> paiVisitIdList = new ArrayList <Long> ();
        Map <Long, List <InpPhamDispenseDetailVO>> dispenseDetailMap = PhamDispenseConvertUtils.convertListToMapByPaivisitID (dispenseDetailList,
                                                                                                                              paiVisitIdList);

        Map <String, List <InpPhamDispenseDetailVO>> dispenseDetailByPhamStdCode = PhamDispenseConvertUtils.convertListToMapByPhamStdCode (dispenseDetailList,
                                                                                                                                           phamStdCodeList);
        context.setNotDispenseApplyDetailList (notDispenseApplyList);
        context.setPaiVisitIdList (paiVisitIdList);
        context.setInpPhamDispenseDetailVOList (dispenseDetailList);
        context.setChangeDispensaryApplyDetail (changeDispensaryApplyDetail);
        context.setDispenseDetailByPaiVisitId (dispenseDetailMap);
        context.setDispenseDetailByPhamStdCode (dispenseDetailByPhamStdCode);
        context.setPhamStdCodeList (phamStdCodeList);
        context.setZeroPhamDispenseDetailList (zeroPhamDispenseDetailList);
        List <InpPhamDispenseDetailVO> totalDispenseList = new ArrayList <InpPhamDispenseDetailVO> ();
        totalDispenseList.addAll (dispenseDetailList);
        totalDispenseList.addAll (zeroPhamDispenseDetailList);
        context.setTotalDispenseList (totalDispenseList);
        return false;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (GenerateDispenseDetail.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
